﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.LogicalGroups.CApLGRGroupManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System.Collections.ObjectModel;
using System.Windows.Forms;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.LogicalGroups;

internal class CApLGRGroupManager
{
  internal CApLGRGroupManager()
  {
  }

  internal static TxTypeFilter LogicalChildrenFilter
  {
    get
    {
      TxTypeFilter logicalChildrenFilter = new TxTypeFilter();
      logicalChildrenFilter.AddIncludedType(typeof (ITxLogicalChild));
      logicalChildrenFilter.AddIncludedType(typeof (TxFrame));
      logicalChildrenFilter.AddIncludedType(typeof (ITxSection));
      logicalChildrenFilter.AddIncludedType(typeof (ITxPointCloud));
      logicalChildrenFilter.AddIncludedType(typeof (ITxSafetyObject));
      logicalChildrenFilter.AddExcludedType(typeof (TxLogicalCollection));
      logicalChildrenFilter.AddExcludedType(typeof (TxHuman));
      return logicalChildrenFilter;
    }
  }

  internal CApLGRGroupCreationData InitGroupCreationDataFromSelection()
  {
    TxObjectList filteredItems = TxApplication.ActiveSelection.GetFilteredItems((ITxTypeFilter) CApLGRGroupManager.LogicalChildrenFilter);
    ITxObject scope = this.GetScope(filteredItems);
    if (scope != null)
      ((Collection<ITxObject>) filteredItems).Remove(scope);
    ITxLogicalGroupCreation parent = scope as ITxLogicalGroupCreation;
    return new CApLGRGroupCreationData(TxApplication.ActiveDocument.LogicalRoot.GetUniqueDisplayName(LGRLogicalGroupResource.CREATE_GROUP_DISPLAY_NAME), filteredItems, (ITxObject) parent);
  }

  internal void CreateNewGroup(CApLGRGroupCreationData lgGroupData)
  {
    ITxLogicalGroupCreation scope = lgGroupData.Scope;
    if (scope == null)
      return;
    TxLogicalGroup txLogicalGroup = (TxLogicalGroup) null;
    Cursor current = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    try
    {
      TxApplication.ActiveUndoManager.StartTransaction();
      TxLogicalGroupCreationData groupCreationData = new TxLogicalGroupCreationData(lgGroupData.Name);
      txLogicalGroup = scope.CreateLogicalGroup(groupCreationData);
      TxObjectList txObjectList = new TxObjectList();
      foreach (ITxObject groupedObject in (Collection<ITxObject>) lgGroupData.GroupedObjects)
      {
        if (txLogicalGroup.CanBeAdded(groupedObject))
          ((Collection<ITxObject>) txObjectList).Add(groupedObject);
      }
      txLogicalGroup.AddObjects(txObjectList);
    }
    catch (TxException ex)
    {
      txLogicalGroup = (TxLogicalGroup) null;
      int num = (int) TxMessageBox.Show(string.Format(LGRLogicalGroupResource.FAILED_TO_CREATE_GROUP, (object) lgGroupData.Name), LGRLogicalGroupResource.CREATE_GROUP_CMD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
    finally
    {
      TxApplication.ActiveUndoManager.EndTransaction();
      Cursor.Current = current;
    }
    if (txLogicalGroup == null)
      return;
    TxObjectList txObjectList1 = new TxObjectList(1);
    ((Collection<ITxObject>) txObjectList1).Add((ITxObject) txLogicalGroup);
    TxApplication.ActiveSelection.SetItems(txObjectList1);
  }

  private ITxObject GetScope(TxObjectList groupedObjects)
  {
    ITxObject scope = (ITxObject) null;
    TxObjectList txObjectList = new TxTypeFilter(typeof (ITxLogicalGroup)).Filter(groupedObjects);
    if (txObjectList != null && ((Collection<ITxObject>) txObjectList).Count == 1)
      scope = ((Collection<ITxObject>) txObjectList)[0];
    return scope;
  }
}
